package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.databases.SettleInfo;
import com.nexgo.payment.databases.mapper.SettleMapper;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;
import com.nexgo.payment.util.StringUtils;

/**
 * 结算
 *
 * Created by xiaox on 2017/9/5.
 */
class SettleBuilder extends DataBuilder {
    SettleBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
    }

    @Override
    SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setMsgType(mTransData.getTransType().getMsgType());
        sendData.setProcCode(mTransData.getTransType().getProcCode());
        sendData.setTraceNum(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        setCurruntTraceNumber(mRepository.getParamValue(PreferencesConstants.TRACE_NUMBER));
        mRepository.increaseTraceNumber();
        sendData.setConditionCode(mTransData.getTransType().getConditionCode());
        sendData.setTerminalId(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        sendData.setMerchantId(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
//        sendData.setAdditionalData(formAdditionalData());
//        sendData.setFundType(mTransData.getFundType());
        sendData.setField60(formField60());
        sendData.setField62(formField62());
        sendData.setField63(formSettleField63());
        sendData.setMac("0000000000000000");
        return sendData;
    }



    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(SysConstant.LOCALE, "%02d",
                Integer.parseInt(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.CURRENT_OPERATOR_NO), "1"))));
        sb.append("0000");
        sb.append("6");
        sb.append(mTransData.isFallback() ? "2" : "0");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }
    //lr
    private String formSettleField63(){
        StringBuilder sb = new StringBuilder();
        SettleInfo settleInfo = new SettleMapper().transform(mRepository.getSuccessfulTransRecords());
        //消费笔数+消费金额
        sb.append(String.format(SysConstant.LOCALE, "%03d", settleInfo.getClassification().getSaleNum()));
        sb.append(String.format(SysConstant.LOCALE, "%012d", settleInfo.getClassification().getSaleAmt()));
        //退货笔数+退货金额
        sb.append(String.format(SysConstant.LOCALE, "%03d", settleInfo.getClassification().getRefundNum()));
        sb.append(String.format(SysConstant.LOCALE, "%012d", settleInfo.getClassification().getRefundAmt()));
        //取款笔数+金额
        sb.append("000000000000000");
        //存款笔数+金额
        sb.append("000000000000000");
        //冲正笔数+金额
        sb.append("000000000000000");
        //转账笔数+金额
        sb.append("000000000000000");
        //代收费笔数+金额
        sb.append("000000000000000");
        //预授权完成笔数+金额
        sb.append(String.format(SysConstant.LOCALE, "%03d", settleInfo.getClassification().getAuthCMNum()));
        sb.append(String.format(SysConstant.LOCALE, "%012d", settleInfo.getClassification().getAuthCMAmt()));
        //拨号成功次数
        sb.append("000");
        //拨号失败次数
        sb.append("000");

        return sb.toString();
    }

    public String formField62() {
        StringBuilder sb = new StringBuilder();
//        sb.append(mTransData.getTransType().getFuncCode());
        sb.append(mRepository.getParamValue(PreferencesConstants.BATCH_NUMBER));
        sb.append(getCurruntTraceNumber());
        return sb.toString();
    }
}
